﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using AccessBasedDesktop.Core;
using log4net;
using log4net.Config;

namespace AccessBasedDesktop.Agent
{
    public partial class MainForm : Form
    {
        private Manager manager;
        private static readonly ILog log = LogManager.GetLogger(typeof(Manager));

        public MainForm()
        {
            InitializeComponent();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (manager != null)
            {
                manager.Options.DesktopSourceDir = txtDesktopSourceDir.Text;
                manager.Options.DomainName = txtDomainName.Text;
                manager.Options.TimerInterval = (int)txtTimerInterval.Value;
                manager.Options.JornalEventCodes = txtJornalEventCodes.Text;
                manager.Options.WatchForChages = chkWatch.Checked;
                manager.Options.ExcludedUsers = txtExcludedUsers.Text;
                manager.Options.ExcludedRoles = txtExcludedGroups.Text;
                manager.Options.SaveToFile();

                log.Info(@"Options saved to file");
            }
            reload();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string configFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
                @"log4net.config.xml");
            XmlConfigurator.Configure(new FileInfo(configFileName));

            log.Info(@"Agent started");

            manager = new Manager(Options.LoadFromFile());
            manager.Options.SaveToFile();
            reload();
            manager.UpdateAllDesktops();
            chkAutoUpdate.Checked = manager.AutoUpdateEnabled;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            reload();
        }

        private void reload()
        {
            manager.Options = Options.LoadFromFile();
            txtDesktopSourceDir.Text = manager.Options.DesktopSourceDir;
            txtDomainName.Text = manager.Options.DomainName;
            txtTimerInterval.Value = manager.Options.TimerInterval;
            txtJornalEventCodes.Text = manager.Options.JornalEventCodes;
            chkWatch.Checked = manager.Options.WatchForChages;
            txtExcludedUsers.Text = manager.Options.ExcludedUsers;
            txtExcludedGroups.Text = manager.Options.ExcludedRoles;
        }

        
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (manager != null)
            {
                manager.Dispose();
                manager = null;
            }
            log.Info(@"Agent exit");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            doUpdate();
        }

        private void doUpdate()
        {
            log.Info(@"Update started by agent user");
            if (manager != null)
                manager.UpdateAllDesktops();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
        }

        private void updateUsersDesktopsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            doUpdate();
        }

        private void chkAutoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            manager.AutoUpdateEnabled = chkAutoUpdate.Checked;
        }

        private void aboutProgramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            (new AboutForm()).Show();
        }
    }
}
